package web.servlet.datasource.student;

import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
 * @Author: 18148
 * @Date: 2020-05-28 18:54:15
 */
public class StudentTest {
    public static Scanner sc = new Scanner(System.in);
    public static StudentDao dao = new StudentDaoDB();

    public static void main(String[] args) {
        while (true) {
            System.out.println("--------欢迎您------");
            System.out.println("1添加学生信息");
            System.out.println("2查询学生信息");
            System.out.println("3更新学生信息");
            System.out.println("4删除学生信息");
            System.out.println("5退出");
            int i = sc.nextInt();
            switch (i) {
                case 1:
                    insertUI();
                    break;
                case 2:
                    selectUI();
                    break;
                case 3:
                    updateUI();
                    break;
                case 4:
                    deleteUI();
                    break;
                case 5:
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入有误！");
                    break;


            }

        }
    }

    private static void insertUI() {
        System.out.println("----------添加界面---------");
        System.out.println("请输入学生姓名：");
        sc.nextLine();
        String name = sc.nextLine();
        System.out.println("请输入学生性别：");
        String sex = sc.nextLine();
        System.out.println("请输入学生年龄：");
        int age = sc.nextInt();
        Student student = new Student(name, sex, age);
        int i = dao.insert(student);
        if (i > 0) {
            System.out.println("添加成功！");
        } else if (i == 0) {
            System.out.println("保存失败！");
        } else {
            System.out.println("学生名称重复");
        }


    }

    private static void selectUI() {
        System.out.println("---------查询界面--------");
        List<Student> students = dao.selectAll();
        if (students != null) {
            Iterator<Student> iterator = students.iterator();
            while (iterator.hasNext()) {
                Student student = iterator.next();
                System.out.println(student);
            }
        }

    }

    private static void updateUI() {
        System.out.println("-----------更新界面-------------");
        System.out.println("请输入要修改学生姓名：");
        sc.nextLine();
        String name = sc.nextLine();
        Student student = dao.selectByName(name);
        if (student == null) {
            System.out.println("学生不存在");
        } else {
            System.out.println(student);
            System.out.println("请输入修改后学生姓名：");
            String name1 = sc.nextLine();
            System.out.println("请输入学生性别：");
            String sex = sc.nextLine();
            System.out.println("请输入学生年龄：");
            int age = sc.nextInt();
            Student student1 = new Student(name1, sex, age);
            int i = dao.update(student.getId(), student1);
            if (i > 0) {
                System.out.println("更新成功！");
            } else {
                System.out.println("更新失败！");
            }
        }
    }

    private static void deleteUI() {
        System.out.println("----------删除界面----------");
        System.out.println("输入要删除的学生名字：");
        sc.nextLine();
        String name = sc.nextLine();
        Student student = dao.selectByName(name);
        if (student == null) {
            System.out.println("要删除的图书不存在！");
        } else {
            int i1 = dao.delete(student.getId());
            if (i1 > 0) {
                System.out.println("删除成功！");
            } else {
                System.out.println("删除失败");
            }
        }

    }
}